package v1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Order is a specification for a Order resource
type Order struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   OrderSpec   `json:"spec"`
	Status OrderStatus `json:"status"`
	Owner  OwnerSpec   `json:"owner"`
}

// OrderSpec is the spec for a Order resource
type OrderSpec struct {
	DeploymentName string `json:"deploymentName"`
	Replicas       *int32 `json:"replicas"`
}

// OrderStatus is the status for a Order resource
type OrderStatus struct {
	AvailableReplicas int32 `json:"availableReplicas"`
	State             int32 `json:"state"`
}
type OwnerSpec struct {
	Name string `json:"name"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// OrderList is a list of Order resources
type OrderList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

	Items []Order `json:"items"`
}
type State int32

const (
	StatePending State = iota
	StateDelivering
	StateDelivered
	StateCancelled
	StateTimeout
	StatePaying
	StatePaid
	StateRefunding
	StateRefunded
	StateProcessing
	StateProcessingFailed
	StateProcessingSucceeded
	StateFailed
	StateSucceeded
	StateUnknown
)

func (s State) String() string {
	switch s {
	case StatePending:
		return "Pending"
	case StateDelivering:
		return "Delivering"
	case StateDelivered:
		return "Delivered"
	case StateCancelled:
		return "Cancelled"
	case StateTimeout:
		return "Timeout"
	case StatePaying:
		return "Paying"
	case StatePaid:
		return "Paid"
	case StateRefunding:
		return "Refunding"
	case StateRefunded:
		return "Refunded"
	case StateProcessing:
		return "Processing"
	case StateProcessingFailed:
		return "ProcessingFailed"
	case StateProcessingSucceeded:
		return "ProcessingSucceeded"
	case StateFailed:
		return "Failed"
	case StateSucceeded:
		return "Succeeded"
	default:
		return "Unknown"
	}
}

func (s State) IsFinal() bool {
	switch s {
	case StateCancelled, StateTimeout, StateRefunded, StateProcessingFailed, StateFailed, StateSucceeded:
		return true
	default:
		return false
	}
}

//
// Product CRD definition (OpenAPI schema)
//
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Product is a specification for a Product resource
type Product struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   ProductSpec   `json:"spec"`
	Status ProductStatus `json:"status"`
}

// ProductSpec is the spec for a Product resource
type ProductSpec struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Price       string `json:"price"`
	Stock       int32  `json:"stock"`
}

// ProductStatus is the status for a Product resource
type ProductStatus struct {
	Available bool   `json:"available"`
	State     string `json:"state"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ProductList is a list of Product resources
type ProductList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

	Items []Product `json:"items"`
}
